import request from './request'
import store from '../store/index'
import config from './config'

Date.prototype.format = function(format) {
	var o = {
		"M+": this.getMonth() + 1, // month
		"d+": this.getDate(), // day
		"h+": this.getHours(), // hour
		"m+": this.getMinutes(), // minute
		"s+": this.getSeconds(), // second
		"q+": Math.floor((this.getMonth() + 3) / 3), // quarter
		"S": this.getMilliseconds()
	}
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
			.substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
	return format;
}
/**
 * 常用工具类
 */
const util = {

	/**
	 * 判断是否为空
	 */
	isEmpty: function(str) {
		return (str == null || str === '' || str === undefined)
	},
	/**
	 * 判断是否不为空
	 */
	isNotEmpty: function(str) {
		return !util.isEmpty(str)
	},
	/**
	 * 判断是否合法的IP
	 */
	isIp: function(ip) {
		const patrn =
			/^(?:(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:1[0-9][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:2[0-5][0-5])|(?:25[0-5])|(?:1[0-9][0-9])|(?:[1-9][0-9])|(?:[0-9]))$/
		return patrn.test(ip)
	},
	isUrl: function(url) {
		const patrn = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/
		return patrn.test(url);
	},
	/**
	 * 验证QQ号码5-11位
	 */
	isQQ: function(qq) {
		var patrn = /^\s*[.0-9]{5,11}\s*$/;
		if (!patrn.test(qq)) {
			return false;
		} else {
			return true;
		}
	},
	/**
	 * 判断手机号是否正确
	 */
	isPhone(telnum) {
		var patrn = /^[1][3,4,5,7,8][0-9]{9}$/;
		if (!patrn.test(telnum)) {
			return false;
		} else {
			return true;
		}
	},
	/**
	 *判断邮箱是否正确
	 */
	isEmail: function(email) {
		//对电子邮件的验证
		var patrn = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		return patrn.test(email)
	},
	/**
	 * 对象克隆
	 */
	extend: function(obj) {
		return Object.assign({}, obj)
	},
	/**
	 * 数组克隆
	 */
	copyArray: function(array) {
		return [...array]
	},

	/**
	 * 判断对象是否是空对象
	 * @param {Object} obj
	 */
	isEmptyObject: function(obj) {
		return obj === undefined || (JSON.stringify(obj) == "{}" );
	},
	/**
	 * 字符串转换JSON对象
	 */
	parseJSON: function(jsonString) {
		return eval('(' + jsonString + ')')
	},
	/**
	 * JSON对象转换字符串
	 */
	toJSONString: function(jsonObj) {
		return JSON.stringify(jsonObj);
	},
	/**
	 * 构建树形结构
	 */
	buildTree: function(paramData, keyName, parentKeyName) {
		const idMap = {}
		// 把所有原始数据的ID作为Key,原始数据对象作为VALUE
		paramData.forEach(item => {
			idMap[item[keyName]] = item
		})

		let val = {}
		const treeData = []
		Object.keys(idMap).forEach(id => {
			val = idMap[id]
			const pid = val[parentKeyName] || val[parentKeyName]
			if (pid === '0' || pid === '' || pid === null || pid === undefined) {
				// 如果是顶层节点，直接添加到结果集合中
				treeData.push(val)
			} else {
				// 如果不是顶层节点，找的父节点，然后添加到父节点的子节点中
				if (idMap[pid] != null) {
					if (!idMap[pid]['children']) {
						idMap[pid]['children'] = []
					}
					idMap[pid]['children'].push(val)
				}
			}
		})
		return treeData
	},
	/**
	 * 构建Picker树形结构
	 */
	buildPickerTree: function(paramData, keyName, parentKeyName) {
		const idMap = {}
		// 把所有原始数据的ID作为Key,原始数据对象作为VALUE
		paramData.forEach(item => {
			idMap[item[keyName]] = item
		})

		let val = {}
		const treeData = []
		Object.keys(idMap).forEach(id => {
			val = idMap[id]
			val['value'] = id
			val['label'] = val.name
			const pid = val[parentKeyName] || val[parentKeyName]
			if (pid === '0' || pid === '' || pid === null || pid === undefined) {
				// 如果是顶层节点，直接添加到结果集合中
				treeData.push(val)
			} else {
				// 如果不是顶层节点，找的父节点，然后添加到父节点的子节点中
				if (idMap[pid] != null) {
					if (!idMap[pid]['children']) {
						idMap[pid]['children'] = []
					}
					idMap[pid]['children'].push(val)
				}
			}
		})
		return treeData
	}


}
export default util
